package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.TourDAO;
import bist.believeinstep.domain.Tour;
import bist.believeinstep.mapper.TourMapper;

public class TourTemplate implements TourDAO, ConnectionDAO{
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(Tour tour){
		String query = "INSERT INTO bist_tours (code, name, avatar, duration, date_start, date_end,"
				+ "content, ticket_number, from, to, is_hot, type, state, created_date, modified_date,"
				+ "created_by, modified_by, bist_tour_prices_id) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
				tour.getCode(),
				tour.getName(),
				tour.getAvatar(),
				tour.getDuration(),
				tour.getDateStart(),
				tour.getDateEnd(),
				tour.getContent(),
				tour.getTicketNumber(),
				tour.getFrom(),
				tour.getTo(),
				tour.getIsHot(),
				tour.getType(),
				tour.getState(),
				tour.getCreated(),
				tour.getModified(),
				tour.getCreatedBy(),
				tour.getModifiedBy(),
				tour.getTourPriceId(),
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(Tour tour){
		String query = "UPDATE bist_tours"
				+ "SET code = ?"
				+ "name = ?"
				+ "avatar = ?"
				+ "duration = ?"
				+ "date_start = ?"
				+ "date_end = ?"
				+ "content = ?"
				+ "ticket_number = ?"
				+ "from = ?"
				+ "to = ?"
				+ "is_hot = ?"
				+ "type = ?"
				+ "state = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?"
				+ "bist_tour_prices_id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				tour.getCode(),
				tour.getName(),
				tour.getAvatar(),
				tour.getDuration(),
				tour.getDateStart(),
				tour.getDateEnd(),
				tour.getContent(),
				tour.getTicketNumber(),
				tour.getFrom(),
				tour.getTo(),
				tour.getIsHot(),
				tour.getType(),
				tour.getState(),
				tour.getCreated(),
				tour.getModified(),
				tour.getCreatedBy(),
				tour.getModifiedBy(),
				tour.getTourPriceId(),	
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_tours WHERE id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(String code){
		String query = "DELETE FROM bist_tours WHERE code = ?";
		try{
			jdbcTemplate.update(query,new Object[]{code});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public Tour getTour(Integer id){
		String query = "SELECT FROM bist_tours WHERE id = ?";
		try{
			Tour tour = jdbcTemplate.queryForObject(query, new Object[]{id}, new TourMapper());
			return tour;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public Tour getTour(String code){
		String query = "SELECT FROM bist_tours WHERE code = ?";
		try{
			Tour tour = jdbcTemplate.queryForObject(query, new Object[]{code}, new TourMapper());
			return tour;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Tour> listTour(){
		String query = "SELECT * FROM bist_tours";
		try{
			List<Tour> tour = jdbcTemplate.query(query, new TourMapper());
			return tour;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
